package com.boarsoft.boar.batch;

import java.util.concurrent.ExecutorService;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.boarsoft.boar.batch.service.BatchDispatcher;
import com.boarsoft.boar.batch.service.BatchInspector;

@Component
public class BatchContext implements ApplicationContextAware, InitializingBean {

	private static ApplicationContext applicationContext;
	private static BatchInspector inspector;
	private static ExecutorService threadPool;

	public static BatchDispatcher getDispatcher(String impl) {
		return (BatchDispatcher) BatchContext.getBean(impl);
	}

	public static boolean containsBean(String name) {
		return applicationContext.containsBean(name);
	}

	public static Object getBean(String name) {
		if (applicationContext == null) {
			return null;
		}
		return applicationContext.getBean(name);
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public void setApplicationContext(ApplicationContext applicationContext) {
		BatchContext.applicationContext = applicationContext;
	}

	public static BatchInspector getInspector() {
		return inspector;
	}

	public void setInspector(BatchInspector inspector) {
		BatchContext.inspector = inspector;
	}

	public static ExecutorService getThreadPool() {
		return threadPool;
	}

	public void setThreadPool(ExecutorService threadPool) {
		BatchContext.threadPool = threadPool;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		setInspector((BatchInspector)applicationContext.getBean("batchInspector"));
	}
}
